package idao.support.spring;

import idao.ConnectionManager;

import java.sql.Connection;

import javax.sql.DataSource;

import org.springframework.jdbc.datasource.DataSourceUtils;

/**
 * ConnectionManager implementation for integration with Spring.
 * 
 * @author huhao
 */
public class SpringAwareConnectionManager implements ConnectionManager
{

    private DataSource ds;

    public SpringAwareConnectionManager(DataSource ds)
    {
        this.ds = ds;
    }

    public Connection getConnection()
    {
        return DataSourceUtils.getConnection(ds);
    }

    /**
     * @see DataSourceUtils#releaseConnection(Connection, DataSource)
     */
    public void returnConnection(Connection connection)
    {
        DataSourceUtils.releaseConnection(connection, ds);
    }

    /**
     * Get the data source used by this handler.
     */
    public DataSource getDataSource()
    {
        return ds;
    }
}
